package com.imchen.googleplay

import android.os.Parcel
import android.util.Log
import de.robv.android.xposed.IXposedHookLoadPackage
import de.robv.android.xposed.XC_MethodHook
import de.robv.android.xposed.XposedBridge
import de.robv.android.xposed.XposedHelpers
import de.robv.android.xposed.callbacks.XC_LoadPackage

/**
 * Created by imch1n on 7/26/18
 */

class Main : IXposedHookLoadPackage {
    override fun handleLoadPackage(lpparam: XC_LoadPackage.LoadPackageParam?) {
        hookSafetyNet(lpparam)
    }

    private fun hookSafetyNet(lpparam: XC_LoadPackage.LoadPackageParam?) {
        val methodName = "createFromParcel"
        val googlePlay = "com.android.vending"
        val cls = "com.google.android.gms.safetynet.n"
        if (!lpparam!!.packageName.equals(googlePlay)) {
            XposedBridge.log("start 0 to hook:" + googlePlay)
            return
        }


        XposedBridge.log("start 1 to hook:" + googlePlay)
        XposedBridge.log("start 2 to hook:" + googlePlay)
        XposedBridge.log("start 3 to hook:" + googlePlay)

        XposedHelpers.findAndHookMethod(cls, lpparam.classLoader, methodName, Parcel::class.java,
                object : XC_MethodHook() {
                    override fun beforeHookedMethod(param: MethodHookParam?) {
                        super.beforeHookedMethod(param)
                        var parcel = param!!.args[0] as Parcel
                        KLog.d("debug->before parcel readString:" + parcel.readString())
                    }

                    override fun afterHookedMethod(param: MethodHookParam?) {
                        super.afterHookedMethod(param)
                        var barr: ByteArray?
                        var i: Int
                        KLog.d("debug->after parcel readString:" + param!!.resultOrThrowable)
                    }
                })

    }

    private fun test() {
        XposedBridge.log("Hello Xposed module!")
    }
}